/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG                        *
*Copyright (C) 2006, Tony Sparks                                                     *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "LUA_Entity.h"
#include "../game/EntityManager.h"
#include "../game/EntityFactory.h"


IEntity* LUA_Entity::m_entity = NULL;

/** Create an Entity */
int LUA_Entity::Create( lua_State *L)
{
    std::string name = luaL_checkstring( L, 1 );
    // create the entity
    m_entity = EntityFactory::GetInstance()->CreatePrefabEntity( name );
    return 0;
}

int LUA_Entity::CreatePlayer( lua_State *L)
{
    std::string name = luaL_checkstring( L, 1 );
    EntityFactory::GetInstance()->CreatePlayer( name );
    return 0;
}

/** Pick and Entity to manipulate */
int LUA_Entity::Pick(lua_State *L)
{
    // retrieve the name from lua
    std::string name = luaL_checkstring( L, 1 );
    // find the entity if possible
    EntityManager* entMng = EntityManager::GetInstance();
    IEntity* ent = entMng->Find( name );
    // make current entity
    if ( ent ) {
        m_entity = ent;
    }
    return 0;
}

/** Set the face of the entity */
int LUA_Entity::SetFace( lua_State *L )
{ 
    int face = (int)luaL_checknumber( L, 1 );
    if ( m_entity )
        m_entity->SetFace( face );
    return 0;
}
/** Set the name of the entity */
int LUA_Entity::SetName( lua_State *L )
{
    // retrieve the name from lua
    std::string name = luaL_checkstring( L, 1 );
    if ( m_entity )
        m_entity->SetName( name );
    return 0;
}
/** Set the state of the entity */
int LUA_Entity::SetState( lua_State *L )
{
    std::string state = luaL_checkstring( L, 1 );
    if ( m_entity )
        m_entity->SetState( state );
    return 0;
}
/** Set the position of the entity */
int LUA_Entity::SetPosition( lua_State *L )
{
    float x = (float)luaL_checknumber( L, 1 );
    float y = (float)luaL_checknumber( L, 2 );
    if ( m_entity )
        m_entity->m_world.Set( x, y );
    return 0;
}
/** Set the speed */
int LUA_Entity::SetVelocity( lua_State *L )
{
    float x = (float)luaL_checknumber( L, 1 );
    float y = (float)luaL_checknumber( L, 2 );
    if ( m_entity )
        m_entity->m_baseVelocity.Set( x, y );
    return 0;
}
/** Add a state */
//int LUA_Entity::AddState( lua_State *L )
//{
//
//    return 0;
//}

/** Get the face of the entity */
int LUA_Entity::GetFace( lua_State *L )
{   
    if ( m_entity ) {
        lua_pushnumber( L, m_entity->GetFace() ); 
        return 1;
    } 
    return 0;
}
/** Get the name of the entity */
int LUA_Entity::GetName( lua_State *L )
{
    if ( m_entity ) {
        lua_pushstring( L, m_entity->GetName().c_str() ); 
        return 1;
    } 
    return 0;
}

/** Get the position of the entity */
int LUA_Entity::GetPosX( lua_State *L )
{
    if ( m_entity ) {
        lua_pushnumber( L, m_entity->m_position.x ); 
        return 1;
    } 
    return 0;
}
/** Get the position of the entity */
int LUA_Entity::GetPosY( lua_State *L )
{
    if ( m_entity ) {
        lua_pushnumber( L, m_entity->m_position.y ); 
        return 1;
    } 
    return 0;
}

/** Get the speed */
int LUA_Entity::GetVelocityX( lua_State *L )
{
    if ( m_entity ) {
        lua_pushnumber( L, m_entity->m_baseVelocity.x ); 
        return 1;
    } 
    return 0;
}

/** Get the speed */
int LUA_Entity::GetVelocityY( lua_State *L )
{
    if ( m_entity ) {
        lua_pushnumber( L, m_entity->m_baseVelocity.y ); 
        return 1;
    } 
    return 0;
}

/** Add a state */
int LUA_Entity::GetCurrentState( lua_State *L )
{
    if ( m_entity ) {
        lua_pushstring( L, m_entity->GetState().c_str() ); 
        return 1;
    } 
    return 0;
}


/** Give the Player an item */
int LUA_Entity::GiveItem( lua_State *L )
{
    if ( m_entity ) {
        std::string entName = luaL_checkstring( L, 1 );
        IEntity* ent = EntityManager::GetInstance()->Find( entName );
        if ( ent )
        {
            // give the player the entity
            m_entity->PickUpItem( ent );
        }
    } 
    return 0;
}

/** Damage an entity */
int LUA_Entity::Damage( lua_State *L )
{
    int dmg = (int)luaL_checknumber( L, 1 );
    if ( m_entity )
        m_entity->Damage( dmg );
    return 0;
}

// -- Bind to LUA
#define method(name) {#name, LUA_Entity::name}

const luaL_reg LUA_Entity::methods[] = {
        method(Create),
        method(CreatePlayer),
        method(Pick),
        method(SetFace),
        method(SetName),
        method(SetState),
        method(SetPosition),
        method(SetVelocity),
       // method(AddState),
        method(GetFace),
        method(GetName),
        method(GetPosX),
        method(GetPosY),
        method(GetVelocityX),
        method(GetVelocityY),
        method(GetCurrentState),
        method(GiveItem),
        method(Damage),
        {NULL, NULL}
};

// - Bind to lua
int luaopen_LUA_Entity(lua_State *L) {
    luaL_openlib(L, "entity", LUA_Entity::methods, 0);
 	return 0;
}